####################################################################################
## 20230302
## 把所有的vcf文件放在一个文件夹下面
mkdir -p ${SigProfiler_path}/vcf_allUSE_AllMut_MSI
mkdir -p ${SigProfiler_path}/vcf_allUSE_AllMut_MSS

mkdir -p ${SigProfiler_path}/extractor_allUSE_AllMut_MSI
mkdir -p ${SigProfiler_path}/extractor_allUSE_AllMut_MSS

mkdir -p ${SigProfiler_path}/decompose_allUSE_AllMut_MSI
mkdir -p ${SigProfiler_path}/decompose_allUSE_AllMut_MSS

## 1、产生输入文件
## NMU的MSS的突变
## 总的 + Trunk + Private
cat ${config_path}/tumor_normal.list | tr ',' ' ' | grep -v Normal | xargs -P 10 -i sh -c '
echo {}
sh ${scripts_path}/sigprofile/denovoSig_1_getPassVcf.NMU.AllVcf.MSI_MSS.AllMut.sh {} ${Images_path}/mutRate ${SigProfiler_path}/vcf_allUSE_AllMut_MSS
'

## NMU的MSI的突变
## 总的 + Trunk + Private
cat ${config_path}/tumor_normal.MSI.list | tr ',' ' ' | grep -v Normal | xargs -P 10 -i sh -c '
echo {}
sh ${scripts_path}/sigprofile/denovoSig_1_getPassVcf.NMU.AllVcf.MSI_MSS.AllMut.sh {} ${Images_path}/mutRateMSI ${SigProfiler_path}/vcf_allUSE_AllMut_MSI
'

nmu_sample=`cat ${config_path}/tumor_normal.class.MSS_MSI.list | awk -F'\t' '{print $3}' | sort -u | grep -v Tumor | tr '\n' '|' | sed 's/|$//'`
## 其它来源的MSS
for sample in `cat ${maf_public_path}/All_use.addVAF.maf | awk -F'\t' '{print $3}' | grep -v -E -w ${nmu_sample} | grep -v Tumor_Sample_Barcode | sort -u`
do
sh ${scripts_path}/sigprofile/denovoSig_1_mafToVCF.AllVcf.MSS.AllMut.sh ${sample}
done
## 其它来源的MSI
for sample in `cat ${maf_public_path}/All_use.addVAF.MSI.maf | awk -F'\t' '{print $3}' | grep -v -E -w ${nmu_sample} | grep -v Tumor_Sample_Barcode | sort -u`
do
sh ${scripts_path}/sigprofile/denovoSig_1_mafToVCF.AllVcf.MSI.AllMut.sh ${sample}
done

## 2、提取突变信号
## 进入python中可以运行，直接运行脚本会有Bug
rm -rf ${SigProfiler_path}/extractor_allUSE_AllMut_MSS/*
${python} ${scripts_path}/sigprofile/denovoSig_2_SigProfiler.AllVcf.MSS.AllMut.py
echo " sleep 2d " | \
qsub -N "denovoSig_MSS" -l nodes=comput13:ppn=50,mem=30gb,walltime=240:00:00 -q smp -d ${Qsub_log_path}

rm -rf ${SigProfiler_path}/extractor_allUSE_AllMut_MSI/*
${python} ${scripts_path}/sigprofile/denovoSig_2_SigProfiler.AllVcf.MSI.AllMut.py
echo " sleep 2d " | \
qsub -N "denovoSig_MSI" -l nodes=comput14:ppn=40,mem=50gb,walltime=240:00:00 -q smp -d ${Qsub_log_path}

############################################################
## 3、denovoToDecompose
${python} ${scripts_path}/sigprofile/denovoSig_3_decompose.py \
${SigProfiler_path}/extractor_allUSE_AllMut_MSI

${python} ${scripts_path}/sigprofile/denovoSig_3_decompose.py \
${SigProfiler_path}/extractor_allUSE_AllMut_MSS

############################################################
## 4、decompose的文件链接
## decompose的信号组成
input_path=extractor_allUSE_AllMut_MSI
output_path=decompose_allUSE_AllMut_MSI

mkdir -p ${SigProfiler_path}/${output_path}
cp -rf ${SigProfiler_path}/${input_path}/decompose/SBS96/Decompose_Solution/Activities/Decompose_Solution_Activities.txt \
${SigProfiler_path}/${output_path}/combine_SBS96.txt
## 总体的突变数量
cp -rf ${SigProfiler_path}/${input_path}/SBS96/Samples.txt ${SigProfiler_path}/${output_path}/combine_SBS96.AllMuts.txt
## decompose的图片
cp -rf ${SigProfiler_path}/${input_path}/SBS96/Suggested_Solution/COSMIC_SBS96_Decomposed_Solution/SBS96_Decomposition_Plots.pdf \
${SigProfiler_path}/${output_path}/SBS96_Decomposition_Plots.pdf
## decompose的图片
cp -rf ${SigProfiler_path}/${input_path}/SBS96/Suggested_Solution/SBS96_De-Novo_Solution/Signatures/SBS_96_plots_SBS96_De-Novo.pdf \
${SigProfiler_path}/${output_path}/SBS_96_plots_SBS96_De-Novo.pdf


input_path=extractor_allUSE_AllMut_MSS
output_path=decompose_allUSE_AllMut_MSS

mkdir -p ${SigProfiler_path}/${output_path}
cp -rf ${SigProfiler_path}/${input_path}/decompose/SBS96/Decompose_Solution/Activities/Decompose_Solution_Activities.txt \
${SigProfiler_path}/${output_path}/combine_SBS96.txt
## 总体的突变数量
cp -rf ${SigProfiler_path}/${input_path}/SBS96/Samples.txt ${SigProfiler_path}/${output_path}/combine_SBS96.AllMuts.txt
## decompose的图片
cp -rf ${SigProfiler_path}/${input_path}/SBS96/Suggested_Solution/COSMIC_SBS96_Decomposed_Solution/SBS96_Decomposition_Plots.pdf \
${SigProfiler_path}/${output_path}/SBS96_Decomposition_Plots.pdf
## decompose的图片
cp -rf ${SigProfiler_path}/${input_path}/SBS96/Suggested_Solution/SBS96_De-Novo_Solution/Signatures/SBS_96_plots_SBS96_De-Novo.pdf \
${SigProfiler_path}/${output_path}/SBS_96_plots_SBS96_De-Novo.pdf

